from rest_framework import serializers

from goods.models import Product, GoodBrand, GoodCategory, IndexCarouselGood
from goods.serializers import GoodParamSerializer
from users.models import UserInfo
from .models import MenuList, RoleList, ReturnGood, ReturnAddress, SystemSet, ReturnReason, Notice, Region

from django.utils import timezone


class NoticeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Notice
        fields = '__all__'


class RegionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Region
        fields = '__all__'


class IndexCarouselGoodSerializer(serializers.ModelSerializer):
    # good = serializers.PrimaryKeyRelatedField(queryset=Product.objects.all(), required=True)
    # pic = serializers.ImageField(default='', required=False, read_only=True)
    # note = serializers.CharField(default='', required=False, read_only=True)
    # add_time = serializers.DateTimeField(default=timezone.now, required=False, read_only=True)

    class Meta:
        model = IndexCarouselGood
        fields = ['name', 'type', 'pic', 'sort', 'status', 'note', 'startTime', 'endTime']


class ListCarouselGoodSerializer(serializers.ModelSerializer):
    # good = serializers.PrimaryKeyRelatedField(queryset=Product.objects.all(), required=True)
    # pic = serializers.ImageField(default='', required=False, read_only=True)
    # note = serializers.CharField(default='', required=False, read_only=True)
    # add_time = serializers.DateTimeField(default=timezone.now, required=False, read_only=True)

    class Meta:
        model = IndexCarouselGood
        fields = '__all__'


class SuperGoodCategorySerializer(serializers.ModelSerializer):
    # sub_cat = GoodCategorySerializer2(many=True)
    # count = serializers.SerializerMethodField(read_only=True)
    #
    # def get_count(self, obj):
    #     return GoodCategory.objects.filter(category_type=3)

    class Meta:
        model = GoodCategory
        fields = '__all__'


class SuperUserInfoSerializer(serializers.ModelSerializer):
    email = serializers.EmailField()

    def create(self, validated_data):
        user = super(SuperUserInfoSerializer, self).create(validated_data=validated_data)
        user.set_password(validated_data["password"])
        user.save()
        return user

    # def update(self, instance, validated_data):
    #     user = managements(SuperUserInfoSerializer, self).create(validated_data=validated_data)
    #     user.set_password(validated_data["password"])
    #     user.save()
    #     return user

    class Meta:
        model = UserInfo
        fields = ['id', 'nickname', 'username', 'email', 'password', 'is_superuser', 'is_staff', 'date_joined',
                  'last_login', 'is_active']


class SuperGoodSerializer(serializers.ModelSerializer):
    # 继承 Serializer 需要手动书写
    # id = serializers.IntegerField(read_only=True)
    # name = serializers.CharField(max_length=100)
    # category = GoodCategorySerializer()
    # title = serializers.CharField(source='name')
    # brandName = serializers.CharField(source='en_brand')
    # images = GoodCarouselImageSerializer(many=True)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    source = serializers.SerializerMethodField(read_only=True)
    brand = serializers.SerializerMethodField(read_only=True)
    category = serializers.SerializerMethodField(read_only=True)
    count = serializers.SerializerMethodField(read_only=True)
    params = GoodParamSerializer(many=True)

    def get_count(self, obj):
        params = Product.objects.get(id=obj.id).params.all()
        return sum(param.sold_num for param in params)

    def get_category(self, obj):
        return Product.objects.get(id=obj.id).category.modify_name

    def get_source(self, obj):
        return Product.objects.get(id=obj.id).source.name

    def get_brand(self, obj):
        base = Product.objects.get(id=obj.id)
        return base.brand.en_name or base.brand.jp_name

    class Meta:
        model = Product
        fields = ['id', 'images', 'sn', 'title', 'modify_title', 'third_url', 'is_fare', 'is_hot', 'is_new',
                  'status',
                  'update_time', 'category', 'brand', 'source', 'count', 'params']


class ReturnReasonSerializer(serializers.ModelSerializer):
    add_time = serializers.DateTimeField(default=timezone.now, required=False, read_only=True)

    class Meta:
        model = ReturnReason
        fields = ['id', 'name', 'sort', 'is_display', 'add_time']


class GoodBrandSerializer(serializers.ModelSerializer):
    good_count = serializers.SerializerMethodField(read_only=True)
    category = serializers.SerializerMethodField(read_only=True)

    def get_category(self, obj):
        return GoodBrand.objects.get(id=obj.id).en_name

    def get_good_count(self, obj):
        return Product.objects.filter(brand_id=obj.id).count()

    class Meta:
        model = GoodBrand
        fields = "__all__"


class ReturnAddressSerializer(serializers.ModelSerializer):
    class Meta:
        model = ReturnAddress
        fields = "__all__"


class SystemSetSerializer(serializers.ModelSerializer):
    class Meta:
        model = SystemSet
        fields = "__all__"


class MenuListSerializer(serializers.ModelSerializer):
    class Meta:
        model = MenuList
        fields = "__all__"


class RoleListSerializer(serializers.ModelSerializer):
    class Meta:
        model = RoleList
        fields = "__all__"
